<?php
/**
 *  发布管理
 *  仲伟涛 2011-4-24
 */
class Admin_Page_Cms_Create extends Admin_Page_Abstract
{
    public function validate(ME_Request $input, ME_Response $output)
    {
        $input->doJump = true;
        $adminInfo = Libs_UcUser::getAdminLoginInfo($input,$output);

        $output->appId =
        $input->appId  = (int)$input->get("appId");
        if(!$input->appId){
            Libs_GlobalFunc::haltMsg("AppId错误！");
        }
        return true;
    }

    private $db;
    private $tblTitleName = 'cms_document_title_';
    private $tblContentName = 'cms_document_content_';
    private function loadDb()
    {
        $this->db = Db_Admin::instance();
    }

    public function doDefault(ME_Request $input, ME_Response $output)
    {
        $output->setTemplate("Cms/Create");
    }

    //生成首页
    public function doIndex(ME_Request $input, ME_Response $output)
    {
        $appId = $input->appId;
        $appInfo = $this->loadCache("Global_App",array());

        $output->appInfo =
        $appInfo = $appInfo[$appId];
        $appPath = $appInfo['enName'];

        //获得APP的配置信息
        $appCfg = ME_Config::get('APP','CONFIG');
        $appCfg = $appCfg[$appId];
        
        $this->loadDb();

        //频道信息
        $output->classInfo =
        $classInfo = $this->loadCache("Cms_Class",array('appId'=>$appId));

        //循环获得所有文章
        $data = array();
        if($classInfo){
            foreach ($classInfo as $cid => $cinfo){

                //获得每个频道的记录
                $param = array(
                        'db'          => $this->db,
                        'fromSql'     => " from {$this->tblTitleName}{$input->appId} ", #from部分的语句
                        'whereSql'   => " where classId = {$cid} and status = 3 ",
                        'orderBySql'  => ' order by datetm desc ',
                        'pagesize'  => 30,
                        'page'      => 1,
                        'onlyData'  => true,
                );
                $data[$cid] = Libs_DBHelper::getList($param);
                //print_r($data[$cid]);
            }
        }
        $output->articleData = $data;

        $tpl = 'Template/1/Cms/Index';

        $output->setTemplate($tpl);
        $html = $output->fetch();
        $path =  PRODUCTION_ROOT . "/Html/{$appPath}/index.shtml" ;
        ME_File::write($html, $path);

        //是否开放wap站点
        if($appCfg['hasWap']){
            $tpl = "Template/{$appId}/CmsWap/Index";
            $output->setTemplate($tpl);
            $html = $output->fetch();
            $path = PRODUCTION_ROOT . "/Html/".$appCfg['wapPath']."/index.wml";
            ME_File::write($html, $path);
        }

        $backUrl = 'http://' . $appCfg['webUrl'];
        Libs_GlobalFunc::haltMsg("首页发布成功！",array('backUrl'=>$backUrl));
    }
    
    //生成列表页
    public function doList(ME_Request $input, ME_Response $output)
    {
        $output->classId =
        $input->classId  = (int)$input->get("classId");
        $page  = (int)$input->get("page");
        $page = max(1,$page);
        if(!$input->classId){
            Libs_GlobalFunc::haltMsg("ClassId错误！");
        }

        
        $rtn = Libs_Cms_Create::publishClass(array(
                                                    'output'  => $output,
                                                    'appId'   => $input->appId,
                                                    'classId' => $input->classId,
                                                    'page'    =>$page
                                                ));


        Libs_GlobalFunc::haltMsg($rtn['name']."列表发布成功！",array('backUrl'=>$rtn["url"]));
        /*exit;

        $appId = $input->appId;
        $appInfo = $this->loadCache("Global_App",array());

        
        //获得APP的配置信息
        $appCfg = ME_Config::get('APP','CONFIG');
        $appCfg = $appCfg[$appId];
        
        //频道信息
        $classInfo = self::$_cache->loadCacheObject("Cms_Class",array('appId'=>$appId));
        $output->classInfo =
        $classInfo = $classInfo[$input->classId];

        $this->loadDb();
        //获得所有记录
        $param = array(
                'db'          => $this->db,
                'fromSql'     => " from {$this->tblTitleName}{$input->appId} ", #from部分的语句
                'whereSql'   => " where classId = {$input->classId} and status = 3 ",
                'orderBySql'  => ' order by datetm desc ',
                'pagesize'  => 200,
                'page'      => $page,
                'urlTpl'    => 'index.php?c='.$input->ctlName.'&appId='.$input->appId.'&page={PAGE}',
        );
        $listData = Libs_DBHelper::getList($param);
        $output->pageBar = $listData['pageBar'];
        $output->DataList = $listData['list'];


        $output->appInfo =
        $appInfo = $appInfo[$appId];
        $appPath = $appInfo['enName'];
        $classPath = $classInfo['path'];

        //是否开放wap站点
        if($appCfg['hasWap']){
            $tpl = "Template/{$appId}/CmsWap/List";
            $output->setTemplate($tpl);
            $html = $output->fetch();
            $path = PRODUCTION_ROOT . "/Html/".$appCfg['wapPath']."/{$classPath}/";
            if($page == 1){
                $path .= "index.wml" ;
            }else{
                $path .= "p_{$page}.wml" ;
            }

            ME_File::write($html, $path);
        }

        $tpl = "Template/{$appId}/Cms/List";
        $output->setTemplate($tpl);
        $html = $output->fetch();

        $path = PRODUCTION_ROOT . "/Html/{$appPath}/{$classPath}/";
        if($page == 1){
            $path .= "index.shtml" ;
        }else{
            $path .= "p_{$page}.shtml" ;
        }

        ME_File::write($html, $path);

        $backUrl = 'http://' . $appCfg['webUrl'] . "/{$classPath}/";
        Libs_GlobalFunc::haltMsg($classInfo['name']."列表发布成功！",array('backUrl'=>$backUrl));
        */

    }

    //生成SiteMap
    public function doSiteMap(ME_Request $input, ME_Response $output)
    {
        $appId = $input->appId;
        Libs_SiteMap_Helper::createCmsSiteMap(array('appId'=>$appId));
        Libs_GlobalFunc::haltMsg("SiteMap.xml 发布成功！",array('stop'=>1));

    }
    
}
